import 'dart:convert';
import 'package:base_lib/common/network/net.dart';
import 'package:base_lib/common/network/result_body.dart';
import 'package:base_lib/config.dart';
import 'package:base_lib/index.dart';
import 'package:doctor_login_lib/repository/http.dart';
import 'package:doctor_prescribe_lib/repository/prescribe_net_intercept.dart';
import '../diagnostic_result/entity/diagnostic_result_list_entity.dart'
    as DiagnosticBean;
import '../western_medicine/medical_search/entity/medical_search_entity.dart'
    as MedicalSearchBean;
import '../western_medicine/edit_medical/entity/medicine_info_entity.dart'
    as MedicineInfoBean;
import '../rp_order/entity/rp_detail_entity.dart' as RpDetailBean;

///开处方
class PrescribeHttp {
  static HttpManager _httpProxyIns;

  static createHttpProxy(
      {mBaseUrl = Config.doctorBaseUrl,
      codeName = Config.codeName,
      codeSuccessValue = Config.codeSuccessValue,
      msgName = Config.msgName,
      reqTimeOut = Config.reqTimeOut,
      respTimeOut = Config.respTimeOut,
      logTag = "prescribe"}) {
    _httpProxyIns = HttpManager(mBaseUrl,
        CODE_NAME: codeName,
        CODE_SUCCESS_VALUE: codeSuccessValue,
        MSG_NAME: msgName,
        REQ_TIME_OUT: reqTimeOut,
        RESP_TIME_OUT: respTimeOut,
        LOG_TAG: logTag,
        defaultInterceptors: [PrescribeNetIntercept()]);
  }

  /// 获取医生的所有医院列表
  static const String INTERFACE_GET_HOSPITAL_LIST_DOCTOR =
      "api/peachUser/hospitalDoctor/getHospitalsByDoctorID";

  /// 查询医生附近的医院列表信息
  static const String INTERFACE_GET_HOSPITAL_LIST =
      "api/peachUser/doctorInfo/getDoctorHospitalAndAroundHospital";

  /// 通过问诊ID获取患者信息
  static const String GET_PATIENT_INFO_BY_DOCTOR =
      "/api/yunInquiry/patientInfo/getPatientInfoByDoctor";

  /// 保存问诊信息
  static const String SAVE_INQUIRY_RESULT =
      "api/yunInquiry/patientInfo/saveInquiryResult";

  /// 获取药品详细信息
  static const String GET_MEDICINE_INFO =
      "/api/medicine/medicineProduct/getMedicineInfo";

  /// 根据坐标查询附近医院
  static const String GET_AROUND_HOSPITAL_BY_POSITION =
      "/api/peachUser/hospitalMng/getAro;undHospitalByPosition";

  /// 搜索药品
  static const String INTERFACE_POST_SEARCHMEDICINE =
      "/api/medicine/medicineGoods/searchGoodsForDoctorByPage";

  /// 保存处方
  static const String INTERFACE_POST_MAKE_RP = "api/rp/prescription/makeRp";

  /// 获取患者ID
  static const String INTERFACE_GET_PRESCRIBE = "api/rp/inquiry/getPrescribe;";

  /// 查询对话人的信息
  static const String INTERFACE_TRANSFER_GET_PERSON_INFO =
      "api/peachUser/pers;onUser/getPersonUserByPersonID";

  /// 保存患者信息
  static const String INTERFACE_POST_PATIENTINFO_SAVEPATIENTINFO =
      "/api/yunInquiry/patientInfo/savePatientInf;o";

  /// 查询药品、处方订单详情
  static const String PHARMACY_GET_ORDER_MEDICINE_OR_RP =
      "api/medicine/goodsOrder/getOrderInfo";

  /// 获取处方笺信息
  static const String PHARMACY_GET_PAY_ORDER_RP_DATA =
      "api/rp/prescription/getRpDetail";

  /// 根据药品ID集合批量查询药品基本信息
  static const String PHARMACY_POST_PAY_ORDER_GOODS_INFO_DETAILS =
      "api/medicine/medicine;Product/getMedicineInfoByList";

  /// 根据药品ID集合批量查询药品图片
  static const String PHARMACY_POST_PAY_ORDER_GOODS_INFO_IMAGES =
      "api/medicine/medicineProduct/getMedicineImgByList";

  /// 根据处方ID查询处方是否有处方订单（及订单的状态）
  static const String PREVIEW_RP_GET_ORDER =
      "api/medicine/goodsOrder/getOrderStatusByRpID";

  /// 线下开处方
  static const String PREVIEW_RP_POST_SAVEOFFLINERP =
      "api/rp/prescription/saveOfflineRp";

  /// 获取处方详情 - 预览处方页面
  static const String INTERFACE_GET_GETRPDETAIL =
      "api/rp/prescription/getRpDetail";

  /// 查询医院名称集合
  static const String INTERFACE_POST_HOSPITAL_NAME_TWO =
      "api/peachUser/hospitalMng/getHospitalNameById";

  /// 查询处方模板
  static const String INTERFACE_GET_GETRPTEMPLATE =
      "api/medicine/rpTemplate/getRpTemplate;";

  /// 新普病种获取
  static const String INTERFACE_GET_XPICD =
      "api/rp/prescription/getXpIcdListByKeyword";

  ///保存患者信息
  static Future<ResultBody> savePatient(
      int inquiryID, String personPatientInfo) {
    return _httpProxyIns.postJson(
      INTERFACE_POST_PATIENTINFO_SAVEPATIENTINFO,
      (resMap) => resMap,
      jsonData:
          '{"inquiryID":"$inquiryID","personPatientInfo":$personPatientInfo}',
    );
  }

  ///获取诊断结果
  static Future<ResultBody<List<DiagnosticBean.Data>>> getXPICD(String orgID,
      {String keyStr = ''}) {
    return _httpProxyIns.get<List<DiagnosticBean.Data>>(
      INTERFACE_GET_XPICD,
      (resMap) {
        return DiagnosticBean.DiagnosticResultListEntity.fromJson(resMap).data;
      },
      queryParams: Map()
        ..['orgID'] = orgID
        ..['keyword'] = keyStr,
    );
  }

  ///西药-搜索药品
  static Future<ResultBody<List<MedicalSearchBean.Datas>>> searchMedicine(
      String searchKey, int orgTypeID, String orgID) {
    return _httpProxyIns.postJson<List<MedicalSearchBean.Datas>>(
      INTERFACE_POST_SEARCHMEDICINE,
      (resMap) =>
          MedicalSearchBean.MedicalSearchEntity.fromJson(resMap).data.datas,
      jsonData: json.encode(Map()
        ..['pageIndex'] = 1
        ..['pageSize'] = 50
        ..['orgTypeID'] = 1 //// 医院的类型, 0: 平台; 1: 医院
        ..['orgID'] = orgID
        ..['medicineClassList'] = [0, 2]
        ..['searchKey'] = searchKey),
    );
  }

  ///中药-搜索药品
  static Future<ResultBody<List<MedicalSearchBean.Datas>>>
      searchChineseMedicine(
          String searchKey, int orgTypeID, String orgID, int medicineFormID) {
    return _httpProxyIns.postJson<List<MedicalSearchBean.Datas>>(
      INTERFACE_POST_SEARCHMEDICINE,
      (resMap) =>
          MedicalSearchBean.MedicalSearchEntity.fromJson(resMap).data.datas,
      jsonData: json.encode(Map()
        ..['pageIndex'] = 1
        ..['pageSize'] = 50
        ..['orgTypeID'] = 1 //// 医院的类型, 0: 平台; 1: 医院
        ..['orgID'] = orgID
        ..['medicineClass'] = 1
        ..['medicineFormID'] = medicineFormID
        ..['searchKey'] = searchKey),
    );
  }

  ///获取药物信息
  static Future<ResultBody<MedicineInfoBean.Data>> getMedicineInfo(
      String medicineID) {
    return _httpProxyIns.get<MedicineInfoBean.Data>(
      GET_MEDICINE_INFO,
      (resMap) => MedicineInfoBean.MedicineInfoEntity.fromJson(resMap).data,
      queryParams: Map()..['medicineID'] = medicineID,
    );
  }

  ///线下开处方
  static Future<ResultBody<String>> postOfflineRp(String jsonStr) {
    //{"code":0,"data":{"rpID":"20102713401630769031329211"},"message":"成功"}
    return _httpProxyIns.postJson<String>(
      PREVIEW_RP_POST_SAVEOFFLINERP,
      (resMap) => resMap['data']['rpID'],
      jsonData: jsonStr,
    );
  }

  ///获取处方详情 - 预览处方页面
  static Future<ResultBody<RpDetailBean.Data>> getRpTemplate(String rpID) {
    //{"code":0,"data":{"rpID":"20102713401630769031329211"},"message":"成功"}
    return _httpProxyIns.get<RpDetailBean.Data>(
      INTERFACE_GET_GETRPDETAIL,
      (resMap) => RpDetailBean.RpDetailEntity.fromJson(resMap).data,
      queryParams: Map()..['inquiryId'] = rpID,
    );
  }
}
